/*
  Copyright (c) 2009-10 by kRaw1er & HierOS
  This file is part of M8J project
*/
#include "CChatList.h"

CChatList::CChatList()
{
}

CChatList::~CChatList()
{
}

int CChatList::CalcItemHeight(int nIndex)
{
	ListItem * pItem = GetItem(nIndex);

	if (pItem)
	{
		CMessageItemData * pItemData = (CMessageItemData*)pItem->Data;
		if (pItemData)
		{
			HDC hdc = CreateCompatibleDC(NULL);
			::SelectObject(hdc, FontHelper::GetFont(GetTextSize()));
			RECT rcText = {0, 0, 480, 20};
			MzDrawText(hdc, pItemData->MessageText.c_str(), &rcText, DT_WORDBREAK | DT_EXPANDTABS | DT_LEFT | DT_TOP | DT_CALCRECT);
			DeleteDC(hdc);
			return RECT_HEIGHT(rcText) + 50;
		}
	}

	return m_nItemHeight;
}

void CChatList::OnRemoveItem(int nIndex)
{
}

void CChatList::DrawItem(HDC hdcDst, int nIndex, RECT * prcItem, RECT * prcWin, RECT * prcUpdate)
{
	ListItem * pItem = GetItem(nIndex);
	if (pItem)
	{
		CMessageItemData * pItemData = (CMessageItemData*)pItem->Data;
		if (pItemData)
		{
			#ifdef CHATLIST_DRAW_MESSAGE_BACK_COLOR
			HBRUSH br = CreateSolidBrush((pItemData->MessageType == MessageSent) ? CHATLIST_BACK_COLOR_MESSAGE_SENT : CHATLIST_BACK_COLOR_MESSAGE_RECEIVED);
			RECT rcBack = *prcItem;
			if (nIndex != 0)
				rcBack.top += 3;
			FillRect(hdcDst, &rcBack, br);
			#endif

			RECT rcImg = *prcItem;
			rcImg.left += 5;
			rcImg.right = rcImg.left + 40;
			rcImg.bottom = rcImg.top + 40;
			if (pItemData->MessageType == MessageReceived)
			{
				IMG_INCOMING_MESSAGE.Draw(hdcDst, &rcImg);
			}
			else if (pItemData->MessageType == MessageSent)
			{
				IMG_OUTCOMING_MESSAGE.Draw(hdcDst, &rcImg);
			}

			RECT rcText = *prcItem;
			rcText.left += 50;
			rcText.bottom = rcText.top + 40;
			SelectObject(hdcDst, FontHelper::GetFont(20));
			::SetTextColor(hdcDst, (pItemData->MessageType == MessageSent) ? CHATLIST_NICKNAME_COLOR_MESSAGE_SENT : CHATLIST_NICKNAME_COLOR_MESSAGE_RECEIVED);
			MzDrawText(hdcDst, pItemData->Name.c_str(), &rcText, DT_LEFT | DT_VCENTER | DT_SINGLELINE | DT_END_ELLIPSIS);

			rcText = *prcItem;
			rcText.right -= 15;
			rcText.bottom = rcText.top + 40;
			SelectObject(hdcDst, FontHelper::GetFont(20));
			::SetTextColor(hdcDst, CHATLIST_DATETIME_COLOR);
			std::wstring str_time = pItemData->DateStr + L' ';
			str_time += pItemData->TimeStr;
			MzDrawText(hdcDst, str_time.c_str(), &rcText, DT_RIGHT | DT_VCENTER | DT_SINGLELINE | DT_END_ELLIPSIS);

			rcText = *prcItem;
			rcText.left += 10;
			rcText.right -= 10;
			rcText.top += 45;
			::SetTextColor(hdcDst, CHATLIST_MESSAGE_TEXT_COLOR);
			MzDrawText(hdcDst, pItemData->MessageText.c_str(), &rcText, DT_LEFT | DT_EXPANDTABS | DT_TOP | DT_WORDBREAK);

			/*
			std::wstring word = L"", smile = L"";
			std::wstring::iterator it = pItemData->MessageText.begin();
			std::wstring::iterator itt;
			RECT rcWord = rcText;
			RECT rcTmp;
			POINT pos;
			SIZE size;
			for ( ; it != pItemData->MessageText.end(); it++)
			{
				switch (*it)
				{
				case L':':
					state = SmileCollecting;
					itt = it;
					++itt;
					if (*itt == L')')
					{
						++it;
						SmilesMap::iterator sm = Smiles.find(L":)");
						if (sm != Smiles.end())
						{
							
							rcWord.right = rcWord.left + sm->second.GetFrameWidth();
							rcWord.bottom = rcWord.top + sm->second.GetFrameHeight();
							sm->second.SetPos(rcWord.left, rcWord.top, RECT_WIDTH(rcWord), RECT_HEIGHT(rcWord));
							sm->second.Start();
							AddChild(&sm->second);
							rcWord.left = rcWord.right;
						}
					}
					break;

				case L'\n':
				case L'\t':
				case L' ':
					if (*it != L'\t' && *it != L'\n')
						word += *it;
					rcTmp.left = rcTmp.top = rcTmp.right = rcTmp.bottom = 0;
					//MzDrawText(hdcDst, word.c_str(), &rcTmp, DT_CALCRECT | DT_LEFT | DT_EXPANDTABS | DT_TOP | DT_NOCLIP);
					GetTextExtentPoint(hdcDst, word.c_str(), word.length(), &size);
					if (rcWord.left + size.cx > RECT_WIDTH(rcText))
					{
						rcWord.left = rcText.left;
						rcWord.top += size.cy;
					}
					
					SelectObject(hdcDst, FontHelper::GetFont(GetTextSize()));
					::SetTextColor(hdcDst, RGB(20,20,20));
					MoveToEx(hdcDst, rcWord.left, rcWord.top, NULL);
					SetTextAlign(hdcDst, GetTextAlign(hdcDst) | TA_UPDATECP);
					ExtTextOut(hdcDst, 0, 0, 0, NULL, word.c_str(), word.length(), NULL);
					//MzDrawText(hdcDst, word.c_str(), &rcWord, DT_LEFT | DT_EXPANDTABS | DT_TOP | DT_NOCLIP);
					SetTextAlign(hdcDst, GetTextAlign(hdcDst) & (~TA_UPDATECP));
					GetCurrentPositionEx(hdcDst, &pos);
					rcWord.left = pos.x;
					word = L"";
					if (*it == L'\n')
					{
						rcWord.left = rcText.left;
						rcWord.top += size.cy;
					}
					else if (*it == L'\t')
					{
						rcWord.left += 8 * GetTextSize() / 2 - rcWord.left % (8 * GetTextSize() / 2);
					}
					break;

				default:
					word += *it;
					itt = it;
					if (++itt == pItemData->MessageText.end())
					{
						MoveToEx(hdcDst, rcWord.left, rcWord.top, NULL);
						SetTextAlign(hdcDst, GetTextAlign(hdcDst) | TA_UPDATECP);
						ExtTextOut(hdcDst, 0, 0, 0, NULL, word.c_str(), word.length(), NULL);
						SetTextAlign(hdcDst, GetTextAlign(hdcDst) & (~TA_UPDATECP));
					}
					break;
				}
			}
			*/
		}
	}
}